package com.yuejian.neusoft.yuejian_24.activity;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.net.Uri;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.squareup.picasso.Picasso;
import com.yuejian.neusoft.yuejian_24.R;
import com.yuejian.neusoft.yuejian_24.javaBean.Collected;
import com.yuejian.neusoft.yuejian_24.javaBean.Success;
import com.yuejian.neusoft.yuejian_24.javaBean.Train;
import com.yuejian.neusoft.yuejian_24.listener.CollectedListener;
import com.yuejian.neusoft.yuejian_24.listener.SuccessListener;
import com.yuejian.neusoft.yuejian_24.listener.TrainListener;
import com.yuejian.neusoft.yuejian_24.model.TrainModel;

public class TrainDetailActivity extends AppCompatActivity implements View.OnClickListener{
    private TextView title,address,time,price,phone,introduction,signup;
    private ImageView img,call,back,collect;
    private Intent intent;
    private Context mContext;
    private SharedPreferences sp;

    TrainListener trainListener = new TrainListener() {
        @Override
        public void onResponse(Train info) {
            title.setText(info.getTrain_name());
            address.setText(info.getAddress());
            time.setText(info.getTime());
            price.setText(info.getPrice()+"￥/人");
            phone.setText(info.getTel());
            introduction.setText("\u3000\u3000"+info.getIntroduction());
            Picasso.with(mContext).load("http://10.0.2.2/yuejian/Public/photo/"+info.getPic()).into(img);
        }

        @Override
        public void onFail() {

        }
    };

    SuccessListener successListener = new SuccessListener() {
        @Override
        public void onResponse(Success success) {
            if (success.getSuccess().equals("1")){
                Toast.makeText(TrainDetailActivity.this,"关注成功", Toast.LENGTH_SHORT).show();
                collect.setImageResource(R.drawable.ic_collect1);
            }else if (success.getSuccess().equals("0")){
                Toast.makeText(TrainDetailActivity.this,"成功取消关注", Toast.LENGTH_SHORT).show();
                collect.setImageResource(R.drawable.ic_collect);
            }
        }

        @Override
        public void onFail() {

        }
    };

    CollectedListener collectedListener = new CollectedListener() {
        @Override
        public void onResponse(Collected collected) {
            if(collected.getCollected().equals("1")){
                collect.setImageResource(R.drawable.ic_collect1);
            }else if(collected.getCollected().equals("0")){
                collect.setImageResource(R.drawable.ic_collect);
            }
        }

        @Override
        public void onFail() {

        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_train_detail);
        initViews();
        initEvent();
        TrainModel trainModel = new TrainModel();
        trainModel.gettrain(getIntent().getStringExtra("trainid"),trainListener);
        trainModel.isCollected(getIntent().getStringExtra("trainid"),sp.getString("uid",""),"4",collectedListener);
    }

    private void initEvent() {
        back.setOnClickListener(this);
        call.setOnClickListener(this);
        collect.setOnClickListener(this);
        signup.setOnClickListener(this);
    }

    private void initViews() {
        title = (TextView)findViewById(R.id.train_detail_title);
        address = (TextView)findViewById(R.id.train_detail_address);
        time = (TextView)findViewById(R.id.train_detail_time);
        price = (TextView)findViewById(R.id.train_detail_price);
        phone = (TextView)findViewById(R.id.train_detail_phone);
        introduction = (TextView)findViewById(R.id.train_detail_introduction);
        signup = (TextView)findViewById(R.id.train_detail_signup);
        img = (ImageView)findViewById(R.id.train_detail_img);
        call = (ImageView)findViewById(R.id.train_detail_call);
        back = (ImageView)findViewById(R.id.train_detail_back);
        collect = (ImageView)findViewById(R.id.train_detail_collect);
        sp = getSharedPreferences("userinfo", Context.MODE_PRIVATE);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.train_detail_back:
                finish();
                break;
            case R.id.train_detail_call:
                Intent intent = new Intent(Intent.ACTION_DIAL, Uri.parse("tel:"+phone.getText().toString()));
                intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                startActivity(intent);
                break;
            case R.id.train_detail_collect:
                TrainModel trainModel = new TrainModel();
                trainModel.follow(getIntent().getStringExtra("trainid"),sp.getString("uid",""),"4",successListener);
                break;
            case R.id.train_detail_signup:
                intent = new Intent(TrainDetailActivity.this,TrainJoinActivity.class);
                intent.putExtra("trainid",getIntent().getStringExtra("trainid"));
                intent.putExtra("uid",sp.getString("uid",""));
                startActivity(intent);
                break;
        }
    }
}
